package com.stars.home.vmodel

import android.app.Application
import android.text.TextUtils
import android.widget.LinearLayout
import androidx.lifecycle.MutableLiveData
import com.stars.home.model.bean.SearchBean
import com.stars.home.utils.PopuShareWxUtils
import com.stars.home.utils.net.NetEngine
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import com.trailer.base.base.BRRepository
import com.trailer.base.base.BRViewModel
import com.trailer.base.net.IResp

class OrderListVModel(application: Application) : BRViewModel<BRRepository>(application) {

    //搜索条件

    val searchBean: MutableLiveData<SearchBean> = MutableLiveData<SearchBean>()


    var infoState: String? = null
    var ll: LinearLayout? =null

    fun getOrderStatus(status: Int,nums:String,carNums:String): String {
        return when (status) {
            10 -> "待支付(0/$nums)"
            20 -> "待接单($carNums/$nums)"
            30 -> "进行中($carNums/$nums)"
            40 -> "已完成($carNums/$nums)"
            100 -> "已取消($carNums/$nums)"
            else -> ""
        }

    }

    fun gotoInfoDetails(info_id: String) {
        //到详情界面
//        UiSwitch.bundle(repository().context, SourceDetailsActivity::class.java, Bundle().apply {
//            putString("info_id", info_id)
//            putInt("isShow", 1)
//        })


    }



    fun toLookImg(qrcode:String){
        if(TextUtils.isEmpty(qrcode)){
            toast("没有二维码！")
            return
        }


        AndPermission.with(repository().context!!)
                .runtime()
                .permission(
                        Permission.WRITE_EXTERNAL_STORAGE,
                        Permission.READ_EXTERNAL_STORAGE,

                        )
                .onGranted { data ->
                    PopuShareWxUtils.show(repository().context!!,qrcode, ll!!)
                }
                .onDenied {
                    toast("读写权限被拒接，请手动打开！")
                }
                .start()

       // PopuShareWxUtils.show(repository().context!!,qrcode, ll!!)


    }

    override fun apiFunc(): suspend () -> IResp<*> {

        var ss = if (searchBean.value?.asc=="-1") {
            "0"
        } else {
            searchBean.value?.asc
        }

        return {NetEngine.service.infoList(
                searchBean.value?.k,
                searchBean.value?.province_out,
                searchBean.value?.city_out,
                searchBean.value?.area_out,
                searchBean.value?.province_in,
                searchBean.value?.city_in,
                searchBean.value?.area_in,
                searchBean.value?.type_id,
                searchBean.value?.s_time1,
                searchBean.value?.s_time2,
                searchBean.value?.r_time1,
                searchBean.value?.r_time2,
                searchBean.value?.price1,
                searchBean.value?.price2,
                searchBean.value?.sort,
                ss,
                infoState,
                searchBean.value?.goods_type,
                repository().page)}
    }

}
